package eu.dzhw.fdz.metadatamanagement.projectmanagement.domain;

import java.time.LocalDateTime;

import javax.validation.constraints.NotEmpty;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import eu.dzhw.fdz.metadatamanagement.common.domain.AbstractRdcDomainObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * Publishing metadata to <a href="https://www.da-ra.de/home/">da|ra</a> will be done asynchronously
 * and repeated as long as the update queue item has not been processed successfully and has
 * therefore been deleted.
 */
@Document(collection = "dara_update_queue_item")
@CompoundIndexes({
    @CompoundIndex(def = "{updateStartedAt: 1, updateStartedBy: 1, createdDate: 1}")
    })
@EqualsAndHashCode(callSuper = false, of = "id")
@ToString(callSuper = true)
@NoArgsConstructor
@Data
@AllArgsConstructor
@Builder
public class DaraUpdateQueueItem extends AbstractRdcDomainObject {

  private static final long serialVersionUID = 5327336410528645215L;

  /**
   * The id of the update queue item. It is generated by the database.
   */
  /* Domain Object Attributes */
  @Id
  private String id;

  /**
   * The id of the {@link DataAcquisitionProject} which needs to be sent to
   * <a href="https://www.da-ra.de/home/">da|ra</a>.
   * 
   * Must not be empty and there must be at most one update queue item in the database for any
   * project.
   */
  @NotEmpty
  @Indexed(unique = true)
  private String projectId;

  /**
   * Timestamp at which the update has been started.
   */
  private LocalDateTime updateStartedAt;

  /**
   * Id of the process who started the updated. Stored in order to avoid having multiple concurrent
   * processes sending data to <a href="https://www.da-ra.de/home/">da|ra</a>.
   */
  private String updateStartedBy;

  /**
   * Construct a dara queue item with a project id.
   * @param projectId The id of a dataPackage.
   */
  public DaraUpdateQueueItem(String projectId) {
    this.projectId = projectId;
  }

}
